package com.mlk.deposit.service.impl;

import com.mlk.core.component.page.Pager;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.SessionUtils;
import com.mlk.deposit.mapper.EngDepositMapper;
import com.mlk.deposit.po.EngDeposit;
import com.mlk.deposit.service.EngDepositService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class EngDepositServiceImpl implements EngDepositService {
    @Resource
    private EngDepositMapper engDepositMapper;

    @Override
    public Pager listPage() {
        // root 用户查看所有
        return Pager.adapt(this.engDepositMapper.selectList(null));
    }


    @Override
    public ServerResponse add(EngDeposit bean) {
        bean.setCreateName(SessionUtils.getUser().getSysUser().getName());
        bean.setCreateTime(new Date());
        int count = this.engDepositMapper.insert(bean);
        if(count == 0){
            return ServerResponse.fail("添加成功");
        }
        return ServerResponse.ok("添加成功");
    }

    @Override
    public ServerResponse selectAll() {
        return ServerResponse.ok(this.engDepositMapper.selectList(null));
    }

    /**
     * 删除暂存区
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ServerResponse del(Integer id) {
        int count = this.engDepositMapper.deleteById(id);
        if(count == 0){
            return ServerResponse.fail("删除失败");
        }
        return ServerResponse.ok("删除成功");
    }
}
